package com.jianxiu.web;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jianxiu.bean.Page;
import com.jianxiu.bean.Result;
import com.jianxiu.bean.SearchParam;
import com.jianxiu.bean.User;
import com.jianxiu.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("list")
    public String list() {
        return "user/list";
    }

    @RequestMapping("list-data")
    @ResponseBody
    public Page<User> listData(SearchParam sp) {
        return Page.create(userService.list(sp.getSp()), userService.count(sp.getSp()));
    }

    @RequestMapping("form")
    public String form(Model model, Long id) {
        model.addAttribute("info", userService.get(id));
        return "user/form";
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(User user) {
        userService.update(user);
        return Result.success();
    }
}
